/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Controllers;

import Model.Lyze;
import Model.Snowboard;
import Model.Zbozi;
import ModelJava.LyzeManager;
import ModelJava.SnowboardManager;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author papa
 */
public class ControllerPridatZbozi extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String viewUrl = "/index.jsp";
        request.setAttribute("showPage", "pridatZbozi");
            HttpSession session = request.getSession();
            if (session == null) {
                viewUrl = "/error.jsp";
                System.out.println("Nastala chyba.");
            } else {
                if (session.isNew()) {
                    System.out.println("Nova Session.");
                }
                if(request.getParameter("zbozi") != null && request.getParameter("zbozi").equals("1")){
                    Snowboard s = createSnb(request);
                    vypis(s);
                    new SnowboardManager().insertData(s);
                    request.setAttribute("showPage", "pridatOK");
                }else if(request.getParameter("zbozi") != null && request.getParameter("zbozi").equals("2")){
                    Lyze l = createLyze(request);
                    new LyzeManager().insertData(l);
                    request.setAttribute("showPage", "pridatOK");
                }
            }

            RequestDispatcher rd = getServletContext().getRequestDispatcher(viewUrl);
            rd.forward(request, response);
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    private Snowboard createSnb(HttpServletRequest request) {
        Snowboard z = new Snowboard();
        Zbozi zb = new Zbozi();
        String tmp;
        zb.setCena(Integer.valueOf(request.getParameter("cena")));
        zb.setDelka(Integer.valueOf(request.getParameter("delka")));
        zb.setExtra(request.getParameter("extra"));
        zb.setNazev(request.getParameter("nazev"));
        zb.setObr("Zbozi/Snowboard/" + request.getParameter("obr"));
        zb.setPocetKusu(Integer.valueOf(request.getParameter("pocetKusu")));
        tmp = request.getParameter("urcenoPro");
        if(tmp.equals("1"))
            zb.setUrcenoPro("muže");
        else if(tmp.equals("2"))
            zb.setUrcenoPro("ženy");
        else zb.setUrcenoPro("děti");
        zb.setZnacka(request.getParameter("znacka"));
        tmp = request.getParameter("jadro");
        if(tmp.equals("1"))
        z.setJadro("dřevěné");
        else z.setJadro("pěnové");
        tmp = request.getParameter("typVazani");
        if(tmp.equals("1"))
            z.setTypVazani("freestyle");
        else z.setTypVazani("deskové");
        z.setZbozi(zb);
        return z;
    }

    private Lyze createLyze(HttpServletRequest request) {
        Lyze z = new Lyze();
        Zbozi zb = new Zbozi();
        String tmp;
        zb.setCena(Integer.valueOf(request.getParameter("cena")));
        zb.setDelka(Integer.valueOf(request.getParameter("delka")));
        zb.setExtra(request.getParameter("extra"));
        zb.setNazev(request.getParameter("nazev"));
        zb.setObr("Zbozi/Lyze/" + request.getParameter("obr"));
        zb.setPocetKusu(Integer.valueOf(request.getParameter("pocetKusu")));
        tmp = request.getParameter("urcenoPro");
        if(tmp.equals("1"))
            zb.setUrcenoPro("muže");
        else if(tmp.equals("2"))
            zb.setUrcenoPro("ženy");
        else zb.setUrcenoPro("děti");
        zb.setZnacka(request.getParameter("znacka"));
        tmp = request.getParameter("jadro");
        if(tmp.equals("1"))
            z.setStyl("skialp");
        else if(tmp.equals("2"))
            z.setStyl("freeride");
        else z.setStyl("carving");
        z.setKrojeni(request.getParameter("krojeni"));
        z.setZbozi(zb);
        return z;
    }

    private void vypis(Snowboard s) {
        System.err.println(s.getZbozi().getCena());
        System.err.println(s.getZbozi().getDelka());
        System.err.println(s.getZbozi().getExtra());
        System.err.println(s.getJadro());
        System.err.println(s.getZbozi().getNazev());
        System.err.println(s.getZbozi().getObr());
        System.err.println(s.getZbozi().getPocetKusu());
        System.err.println(s.getTypVazani());
        System.err.println(s.getZbozi().getUrcenoPro());
        System.err.println(s.getZbozi().getZnacka());
    }

}
